package com.youmu.shop.coupon.controller;


import java.util.List;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.exception.ParamException;
import com.result.Result;
import com.result.ResultUtils;
import com.youmu.shop.context.utils.UserContext;
import com.youmu.shop.coupon.service.CouponService;
import com.youmu.shop.coupon.vo.CouponVO;
import com.youmu.shop.param.CalculateFreightParam;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;


@RestController
@RequestMapping(value = "/coupon/remote/")
public class CouponController {

    
	@Resource
    private CouponService couponService;
    
	@ApiOperation(value = "获取优惠劵")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "userId", value = "用户id", required = true, dataType = "long", paramType = "query"),
			@ApiImplicitParam(name = "status", value = "状态 0可使用，1已使用，2已过期", required = true, dataType = "long", paramType = "query"),
	})
   	@RequestMapping(value = "findCoupon",method=RequestMethod.GET)
   	public Result<CouponVO> findCoupon(Long userId,Integer status) {
		if(userId == null){
			throw new ParamException("userId不能为空");
		}
		if(status == null){
			throw new ParamException("status不能为空");
		}
//       	List<CouponVO>  couponVOs = couponService.findCoupon(userId,status);
       	List<CouponVO>  couponVOs = couponService.findCoupon(userId,status);
       	return ResultUtils.createDefResult(couponVOs);
   	}
	
	@ApiOperation(value = "根据优惠劵id获取优惠券抵扣")
	@RequestMapping(value = "getCouponByCouponCodeId",method=RequestMethod.GET)
	@ApiImplicitParams({
		@ApiImplicitParam(name = "id", value = "优惠劵id", required = true, dataType = "Long", paramType = "query")
    })
	public Result<Double> getCouponByCouponCodeId(Long id){
		Double priceExpression = couponService.getCouponByCouponCodeId(id);
		return ResultUtils.createDefResult(priceExpression);
	}
	
	/**
	 * 下单页面点击优惠券查询
	 * @return
	 */
	@ApiOperation(value = "下单页面点击优惠券查询")
	@RequestMapping(value = "selectCouponsForPlaceOrder", method = RequestMethod.POST)
	public Object selectCouponsForPlaceOrder(CalculateFreightParam calculateFreightParam){
		calculateFreightParam.setUserId(UserContext.getUserInfo().getId());
		return ResultUtils.createDefResult(couponService.selectCouponsForPlaceOrder(calculateFreightParam));
	}
	
}